package com.itany.corejava.day14;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileSplitler {

    private JFrame jf;
    private JLabel jl1;
    private JLabel jl2;
    private JTextField jtf1;
    private JTextField jtf2;
    private JButton jb1;
    private JButton jb2;
    private JButton jb3;

    public FileSplitler() {

        jf=new JFrame("文件分割器");
        jl1=new JLabel("文件：");
        jl2=new JLabel("大小：");
        jtf1=new JTextField(20);
        jtf2=new JTextField(5);
        jb1=new JButton("打开");
        jb2=new JButton("分割");
        jb3=new JButton("合并");

    }

    private void init(){
        JPanel jp1=new JPanel();
        jp1.add(jl1);
        jp1.add(jtf1);
        jp1.add(jb1);

        JPanel jp2=new JPanel();
        jp2.add(jl2);
        jp2.add(jtf2);
        jp2.add(jb2);
        jp2.add(jb3);

        jf.add(jp1, BorderLayout.NORTH);
        jf.add(jp2);
    }

    private void addEventHandler(){
        jb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                FileDialog fd=new FileDialog(jf,"打开文件");
                fd.setVisible(true);
                if(fd.getFile()!=null){
                    jtf1.setText(fd.getDirectory()+fd.getFile());
                }
            }
        });

        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if("".equals(jtf1.getText())||"".equals(jtf2.getText())){
                    JOptionPane.showMessageDialog(jf,"信息不完整");
                    return;
                }

                FileInputStream fis=null;
                try{

                    fis=new FileInputStream(jtf1.getText());


                    int n;
                    int i=0;

                    byte[] b=new byte[Integer.parseInt(jtf2.getText())];

                    while((n=fis.read(b))!=-1){
                        FileOutputStream fos=null;
                        try{
                            fos=new FileOutputStream(jtf1.getText()+(i++));

                            fos.write(b,0,n);
                        }catch (Exception ex){
                            throw ex;
                        }finally{
                            if(fos!=null){
                                fos.close();
                            }
                        }
                    }



                }catch (NumberFormatException ex){
                    JOptionPane.showMessageDialog(jf,"大小必须是数字");
                }catch (Exception ex){
                    JOptionPane.showMessageDialog(jf,"分割失败");
                }finally{
                    if(fis!=null){
                        try {
                            fis.close();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            }
        });

        jb3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if("".equals(jtf1.getText())){
                    JOptionPane.showMessageDialog(jf,"信息不完整");
                    return;
                }

                FileOutputStream fos=null;
                try{

                    fos=new FileOutputStream(jtf1.getText());

                    int i=0;

                    while(true){

                        File file=new File(jtf1.getText()+(i++));

                        if(file.exists()==false){
                            break;
                        }

                        FileInputStream fis=null;
                        try{

                            fis=new FileInputStream(file);

                            byte[] b=new byte[fis.available()];

                            int n=fis.read(b);

                            fos.write(b,0,n);

                        }catch (Exception ex){
                            throw ex;
                        }finally{
                            if(fis!=null){
                                fis.close();
                            }
                        }


                    }



                }catch (Exception ex){
                    JOptionPane.showMessageDialog(jf,"合并失败");
                }finally{
                    if(fos!=null){
                        try {
                            fos.close();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                }


            }
        });
    }

    public void show(){
        init();
        addEventHandler();
        jf.pack();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new FileSplitler().show();
    }
}
